package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageResult;
import cn.wolfcode.domain.Result;
import cn.wolfcode.exception.NoLoginException;
import cn.wolfcode.mapper.EmpMapper;
import cn.wolfcode.service.EmpService;
import cn.wolfcode.util.JWTUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public PageResult query(String name, Integer gender, Date begin, Date end, Integer pageNum, Integer pageSize) {
       PageHelper.startPage(pageNum, pageSize);
       List<Emp> list = empMapper.query(name, gender, begin, end);
       Page<Emp> page =(Page<Emp>) list;
       return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public Emp getById(Long id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(new Date());
        empMapper.update(emp);

    }
    @Override
    public void add(Emp emp) {
        emp.setCreateTime(new Date());
        emp.setUpdateTime(new Date());
        empMapper.add(emp);
    }

    @Override
    public void delete(List<Long> ids) {
        empMapper.delete(ids);

    }
    @Override
    public Result login(Emp loginemp) {
        Emp emp = empMapper.login(loginemp);
        if(emp != null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",emp.getId());
            claims.put("username",emp.getUsername());

            return Result.success(JWTUtils.createToken(claims));
        }
        else{
            throw new NoLoginException("失恋了吗小狗 密码错误");
        }



    }
}

